//
//  GCMMainHttpRequst.m
//  GeeCityManager
//
//  Created by 聚城科技 on 16/3/15.
//  Copyright © 2016年 聚城科技. All rights reserved.
//

#import "GCMMainHttpRequst.h"

@implementation GCMMainHttpRequst


+ (NSURLSessionDataTask *)pushTestSuccess:(void (^) (id MainPage , NSError *))success{
    NSMutableDictionary *parameters= [[NSMutableDictionary alloc]init];
    parameters[@"followname"] = @"系统管理员";
    parameters[@"id"] = @"1";
    
    return [[CZHAppNetAPIClient sharedClient] POST:@"push/push.php" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSString *str=[[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSData *jsonData = [str dataUsingEncoding:NSUTF8StringEncoding];
        
        NSDictionary *responseData = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
//        NSLog(@"pppppppppppppp%@",responseObject);
//        NSLog(@"++1+  %@",responseData);
        if (success) {
            
            success(responseData,nil);
            NSLog(@"++2+  %@",responseData);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"++3+  %@",error);
        if (success) {
            success(nil, error);
            NSLog(@"++4+  %@",error);
        }
    }];

    
}

+ (NSURLSessionDataTask *)postMainPageInfoWithUserId:(NSString *)uid WithDepartID:(NSString *)departId WithuserName:(NSString *)userName Success:(void (^) (id MainPage , NSError *))success{

    NSMutableDictionary *parameters= [[NSMutableDictionary alloc]init];
    parameters[@"departId"] = departId;
    parameters[@"userId"] = uid;
    parameters[@"userName"] = userName;
//    userName = 系统管理员,
//    userId = 42,
//    departId = 13
    
//    NSLog(@"//////%@",parameters);
    return [[CZHAppNetAPIClient sharedClient] POST:MAIN_URL parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSDictionary *responseData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
//        NSLog(@"++1+  %@",responseData);
        if (success) {
            success(responseData,nil);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"++2+  %@",error);
        if (success) {
            success(nil, error);
        }
    }];
    
}
+ (NSURLSessionDataTask *)postMain2PageInfoWithUserId:(NSString *)uid WithDepartID:(NSString *)departId WithuserName:(NSString *)userName Success:(void (^) (id MainPage , NSError *))success{
    
    NSMutableDictionary *parameters= [[NSMutableDictionary alloc]init];
    parameters[@"departId"] = departId;
    parameters[@"userId"] = uid;
    parameters[@"userName"] = userName;

    return [[CZHAppNetAPIClient sharedClient] POST:MAIN2_URL parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSDictionary *responseData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        //        NSLog(@"++1+  %@",responseData);
        if (success) {
            success(responseData,nil);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"++2+  %@",error);
        if (success) {
            success(nil, error);
        }
    }];
    
}

//统计app的点击次数
+ (NSURLSessionDataTask *)ClickTimes:(NSString *)typeid Success:(void (^) (id times , NSError *))success{

    NSMutableDictionary *parameters= [[NSMutableDictionary alloc]init];
    
    parameters[@"typeid"] = typeid;
    parameters[@"company"] = [GCMUserInfo instance].company;
    parameters[@"cellsystem"] = @"1";
    parameters[@"phonetype"] = [GCMMainHttpRequst deviceVersion];
    parameters[@"ipaddress"] = [GCMMainHttpRequst getDeviceIPIpAddresses];
    parameters[@"loginname"] = [GCMUserInfo instance].realName;
//    NSLog(@"++1+  %@",parameters);
    return [[CZHAppNetAPIClient sharedClient] POST:CLICKCOUNT parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        //NSData 转NSString
        NSString *result  =[[ NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        if (success) {
            success(result,nil);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"++2+  %@",error);
        if (success) {
            success(nil, error);
        }
    }];
    
}

//判断手机型号
+ (NSString*)deviceVersion{
    // 需要#import "sys/utsname.h"
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    
    //CLog(@"%@",deviceString);
    
    if ([deviceString isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";
    if ([deviceString isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
    if ([deviceString isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
    if ([deviceString isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
    if ([deviceString isEqualToString:@"iPhone3,2"])    return @"Verizon iPhone 4";
    if ([deviceString isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
    if ([deviceString isEqualToString:@"iPhone5,2"])    return @"iPhone 5";
    if ([deviceString isEqualToString:@"iPhone6,2"])    return @"iPhone 5S";
    if ([deviceString isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";
    if ([deviceString isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";
    if ([deviceString isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";
    if ([deviceString isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";
    if ([deviceString isEqualToString:@"iPad1,1"])      return @"iPad";
    if ([deviceString isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
    if ([deviceString isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";
    if ([deviceString isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
    if ([deviceString isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
    if ([deviceString isEqualToString:@"i386"])         return @"Simulator";
    if ([deviceString isEqualToString:@"x86_64"])       return @"Simulator";
    
    //CLog(@"NOTE: Unknown device type: %@", deviceString);
    
    return deviceString;
}

//获取ip
+ (NSString *)getDeviceIPIpAddresses{
    
    int sockfd =socket(AF_INET,SOCK_DGRAM, 0);
    
    //    if (sockfd <</span> 0) return nil;
    
    NSMutableArray *ips = [NSMutableArray array];
    
    
    
    int BUFFERSIZE =4096;
    
    struct ifconf ifc;
    
    char buffer[BUFFERSIZE], *ptr, lastname[IFNAMSIZ], *cptr;
    
    struct ifreq *ifr, ifrcopy;
    
    ifc.ifc_len = BUFFERSIZE;
    
    ifc.ifc_buf = buffer;
    
    if (ioctl(sockfd,SIOCGIFCONF, &ifc) >= 0){
        
        for (ptr = buffer; ptr < buffer + ifc.ifc_len; ){
            
            ifr = (struct ifreq *)ptr;
            
            int len =sizeof(struct sockaddr);
            
            if (ifr->ifr_addr.sa_len > len) {
                
                len = ifr->ifr_addr.sa_len;
                
            }
            
            ptr += sizeof(ifr->ifr_name) + len;
            
            if (ifr->ifr_addr.sa_family !=AF_INET) continue;
            
            if ((cptr = (char *)strchr(ifr->ifr_name,':')) != NULL) *cptr =0;
            
            if (strncmp(lastname, ifr->ifr_name,IFNAMSIZ) == 0)continue;
            
            memcpy(lastname, ifr->ifr_name,IFNAMSIZ);
            
            ifrcopy = *ifr;
            
            ioctl(sockfd,SIOCGIFFLAGS, &ifrcopy);
            
            if ((ifrcopy.ifr_flags &IFF_UP) == 0)continue;
            
            
            
            NSString *ip = [NSString stringWithFormat:@"%s",inet_ntoa(((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr)];
            
            [ips addObject:ip];
            
        }
        
    }
    
    close(sockfd);
    
    
    
    
    
    NSString *deviceIP =@"";
    
    for (int i=0; i < ips.count; i++)
        
    {
        
        if (ips.count >0)
            
        {
            
            deviceIP = [NSString stringWithFormat:@"%@",ips.lastObject];
            
            
            
        }
        
    }
    
//    NSLog(@"deviceIP========%@",deviceIP);
    return deviceIP;
    
}

@end
